Function ldss3_subbias,frame,biasf

;+------------------------------------------------------------------------
;
; LDSS3_SUBBIAS				10/2011 BY JRG 
;
; Takes a raw ldss3 frame and subtract the bias. 
; There are 2 modes at the moment : individual overscan regions 
; or a superbias. The parameter 'biasf' controls which mode 
; you use. 
; 
;
; INPUTS
;   
;    frame  : raw frame or the list of bias frames 
;    biasf  : = 0 if use overscan region of each frame 
; 	        : = 1 if use a set of bias frames (frame is the list) 
; 
;
;
; OUTPUTS 
;   
;   bias-subtracted frame. 
;
;
; FUNCTIONS/PROCEDURES CALLED
; 
;    robust_sigma() : (idlutils+goddard) 
;
;
; FUTURE IMPROVEMENTS ?
; 
;   Create a polynomial fit in x- and y- directions and subtract the 
;   resulting fit. 
;
;
; HISTORY 
;  
;  Written by JRG 10/06/2011
; 
;
;+------------------------------------------------------------------------
if (n_params() eq 0) then begin
   print,'CALLING SEQUENCE: result = ldss3_subbias(frame)'
   return,-1
endif

; reads in all the parameters : 
@params.list 

print,'biasf =',biasf

; read in the data file 
dat=readfits(frame+'.fits',head)
subframe=1.0*dat
sizeframe=size(dat)

; use the overscan region of each frame : 
if biasf eq 0 then begin 
 szy=sizeframe(2) 
 szx=sizeframe(1)
 print,'szx =',szx
 print,'szy =',szy
 print,'o_xmin =',o_xmin 
 print,'o_xmax =',o_xmax
 print,'o_ymin =',o_ymin 
 print,'o_ymax =',o_ymax 
 overscan=subframe(o_xmin:o_xmax,*)
 ; measure the mean (along the x direction) in the overscan region,
 ; rejects all pixels > 4 sigma 
 ; calculates the mean without outliers 
 ; subtract  the mean of the overscan region from each line
 for i=0,szy-1 do begin 
    reg=overscan(*,i)
    med_reg=median(reg) 
    if verbose eq 1 then begin 
   ;  print,'i =',i,' med_reg =',med_reg
    endif
    idok=where(abs(reg-med_reg) lt 3.0*robust_sigma(reg))   
    mean_reg=mean(reg(idok))
    if verbose eq 1 then begin 
 ;     print,'mean =',mean_reg
    endif
    subframe(*,i)=subframe(*,i)-mean_reg
 endfor 
 overscan=subframe(*,o_ymin:o_ymax)
 for i=0,szx-1 do begin 
   reg=overscan(i,*)
   med_reg=median(reg) 
   if verbose eq 1 then begin 
  ;   print,'i =',i,' med_reg =',med_reg
   endif
   idok=where(abs(reg-med_reg) lt 3.0*robust_sigma(reg))   
   mean_reg=mean(reg(idok))
   if verbose eq 1 then begin 
  ;    print,'mean =',mean_reg
    endif
    subframe(i,*)=subframe(i,*)-mean_reg
 endfor
endif 
 
; use the specified bias frames : 
if biasf eq 1 then begin 
; templ =  {   $
;          version: 1.0, $
;          datastart: 0L, $
;          delimiter: ' ', $
;          missingvalue: -99.0, $
;          commentsymbol: '#', $
;          fieldcount: 1L, $
;          fieldtypes: [7L], $
;          fieldnames: ['filename'], $
;          fieldlocations: [6L], $
;          fieldgroups: [0L] $
;        }
; list    = read_ascii(biasframe,template=templ)
 objname = biasl
 nobj    = n_elements(objname)
 ; create the staked bias frame : 
 big_bias=dblarr(sizeframe(1),sizeframe(2),nobj)
 bias_med=dblarr(sizeframe(1),sizeframe(2))
 ; read all bias frames : 
 for i=0,nobj-1 do begin 
  big_bias(*,*,i)=readfits(objname(i)+'.fits')
 endfor 
  ; median-combined the frames : 
  bias_med=median(big_bias,dim=3,/even)
  sizedat=size(dat)
  szx=sizedat(1)
  szy=sizedat(2)
  ; subtract the bias along the lines : 
  for i=0,szy-1 do begin 
    temp=bias_med(*,i)
    med_reg=median(temp) 
    if verbose eq 1 then begin 
   ;  print,'i =',i,' med_reg =',med_reg
    endif
    idok=where(abs(temp-med_reg) lt 3.0*robust_sigma(temp))   
    mean_reg=mean(temp(idok))
    if verbose eq 1 then begin 
    ;  print,'mean =',mean_reg
    endif
    subframe(*,i)=subframe(*,i)-mean_reg
    bias_med(*,i)=bias_med(*,i)-mean_reg
 endfor 
 
 ; subtract the bias along the columns : 
 for i=0,szx-1 do begin
    temp=bias_med(i,*)
    med_reg=median(temp) 
    if verbose eq 1 then begin 
  ;   print,'i =',i,' med_reg =',med_reg
    endif
    idok=where(abs(temp-med_reg) lt 3.0*robust_sigma(temp))   
    mean_reg=mean(temp(idok))
    if verbose eq 1 then begin 
  ;    print,'mean =',mean_reg
    endif
    subframe(i,*)=subframe(i,*)-mean_reg
    bias_med(i,*)=bias_med(i,*)-mean_reg
 endfor 
endif

 ; if debug file (see params.list) is set to 1 
 ; write bias-subtracted frame to disc. 
 if debug eq 1 then begin
  writefits,'caca.fits',subframe
 endif 

return,subframe
end
